﻿using System;
using System.Collections.Generic;
using System.Text;

namespace TinyUrlPlugin
{
    public class TinyUrlPlugin : Plugins.IPlugin
    {
        public static string MakeTinyUrl(string Url)
        {
            try
            {
                if (Url.Length <= 30) return Url;
            
                if (!System.Uri.IsWellFormedUriString(Url, UriKind.RelativeOrAbsolute)) return Url;
                string url = "http://tinyurl.com/api-create.php?url={0}";
                string tinyUrl = Unified.Network.HTTP.Web.HTTPAsString(string.Format(url, Url));
                return tinyUrl;
            }catch(Exception Exc) {
                System.Diagnostics.Trace.WriteLine(Exc.ToString());
            }
            return Url;
        }

        #region IPlugin Members

        public List<string> Command
        {
            get { return new List<string>() { "tinyurl", "t", "shorten" }; }
        }

        public string RootStore { get; set; }

        public string Name
        {
            get { return "TinyUrl Creater"; }
        }

        public MSNPSharp.Messenger Messenger { get; set; }

        public string Chat(string Command, string UserID, string Input)
        {
            return MakeTinyUrl(Input);
        }

        #endregion
    }
}